const  chesspieces = new Array("Black", "White", "Empty");
const  CHESS_BLACK = 0;
const  CHESS_WHITE = 1;
const  CHESS_EMPTY = 2;
const  CHESS_WHITE_WIN_FLAG = "" + CHESS_WHITE + CHESS_WHITE + CHESS_WHITE + CHESS_WHITE + CHESS_WHITE;
const  CHESS_BLACK_WIN_FLAG = "" + CHESS_BLACK + CHESS_BLACK + CHESS_BLACK + CHESS_BLACK + CHESS_BLACK;

function test(){
 var a=0;
}

var ChessInfo = {
	chessboardrows: 15,
	chessboardcolumns: 15,
	chessstate: 0,
	chessflag: 0, /* 0 - means white chess piece; 1 - means black chess piece; 2 - means empty chess piece; */
	
	CHESS_STATE_STOPING: 0,
	CHESS_STATE_PLAYING: 1,
	
	init: function(scene) {
		this.parentscene = scene;
		this.chesstable = this.parentscene.controller.get('chesstable');
		this.chesssteps = new ChessSteps();
		this.chessstate = this.CHESS_STATE_PLAYING;
		ChessScan.init();
		this.cbtracker = new ChessBoardTracker( );
		this.cbtracker.init();
	},
	
	cellOnClick: function(cell) {
		cell.onclick = function() {
			if (ChessInfo.chessstate == ChessInfo.CHESS_STATE_PLAYING) {
				if (this.getAttribute("chess") == CHESS_EMPTY) {
					ChessInfo.moveForward(cell);
				}
			}
		}
	},

	buttonRestartOnClick: function(button) {
		button.onclick = function() {
			ChessInfo.reStartGame();
		}
	},
	
	buttonRegretOnClick: function(button) {
		button.onclick = function() {
			if (ChessInfo.chessstate == ChessInfo.CHESS_STATE_PLAYING){
				ChessInfo.regretGame(1);
			}
		}
	},
	
	cellFill: function(cell, chess) {
		cell.innerHTML = "<div class='chesspiece chesspiece-"+ chesspieces[chess] + "'></div>";
		cell.setAttribute("chess", chess);
	},

	getCell: function(x, y) {
		return this.chesstable.rows[y].cells[x];
	},
	
	moveForward: function(cell) {
		var x = cell.cellIndex;
		var y = parseInt(cell.getAttribute("rowIndex"));
		var chess = this.chessflag % 2;
		
		this.cellFill(cell, chess);
		this.cbtracker.setChess(x, y, chess);
		this.chesssteps.addStep(x, y);		
		
		if (this.isWhoWin(x, y)) {
			this.showWinDialog();
		} else {
			this.chessflag++;
		}
	},
	
	regretGame: function(steps) {
		for (var i = 0; i < steps; i++) {
			if (this.chesssteps.backStep()) {			
				this.cellFill(this.getCell(this.chesssteps.x, this.chesssteps.y), CHESS_EMPTY);
				this.cbtracker.setChess(this.chesssteps.x, this.chesssteps.y, CHESS_EMPTY)
				if (this.chessflag > 0) this.chessflag--;
			}
		}
	},
	
	reStartGame: function() {
		this.chessflag = 0;
		this.chessstate = this.CHESS_STATE_PLAYING;
		for (var i = 0; i < this.chessboardrows; i++) {
			for (var j = 0; j < this.chessboardcolumns; j++) {
				this.cellFill(this.getCell(j, i), CHESS_EMPTY);
				this.cbtracker.setChess(j, i, CHESS_EMPTY);
			}
		}		
	},

	isWhoWin: function(x, y) {
		var chess = this.chessflag % 2;
		
		for (var i = 0; i < ChessScan.SCAN_DIR_COUNTS; i++) {
			var result = ChessScan.scan(x, y, i);

			switch (chess) {
				case CHESS_WHITE:
				if (result.indexOf(CHESS_WHITE_WIN_FLAG) != -1) {
					return true;
				}
				break;
				
				case CHESS_BLACK:
				if (result.indexOf(CHESS_BLACK_WIN_FLAG) != -1) {
					return true;
				}
				break;
				
				default:
				break;	
			}
		}		
		return false;
	},
	
	
	showWinDialog: function() {
		this.parentscene.controller.showAlertDialog({
	        onChoose: function(value) {
				if (value == "OK") {
					ChessInfo.reStartGame();					
				} else {
					ChessInfo.chessstate = ChessInfo.CHESS_STATE_STOPING;
				}
			},
	        title: $L( "Congratulation!"),
	        message: $L("After " + (ChessInfo.chessflag + 1) + " steps, " + chesspieces[ChessInfo.chessflag % 2] + " Chess won ! Play again?"),
	        choices:[
	             {label:$L('OK'), value:"OK", type:'affirmative'},
	             {label:$L('Cancel'), value:"Cancel", type:'affirmative'}
	        ]
	        });
	},


	showDebugDialog: function(msg) {
		this.parentscene.controller.showAlertDialog({
	        onChoose: function(value) {
				if (value == "OK") {
					;
				}
			},
	        title: $L( "Debug"),
	        message: $L(msg),
	        choices:[
	             {label:$L('OK'), value:"OK", type:'affirmative'}
	        ]
	        });
	}
};

